#include <iostream>
#include <vector>
#include "listnode.h"
using namespace std;

class Solution {
public:
    ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
    {
        ListNode *result = new ListNode(0);
        ListNode *p = result;
        while (pHead1 != nullptr && pHead2 != nullptr) {
            if (pHead1->val < pHead2->val) {
                p->next = pHead1;
                pHead1 = pHead1->next;
            } else {
                p->next = pHead2;
                pHead2 = pHead2->next;
            }
            p = p->next;
        }
        if (pHead1 == nullptr) {
            p->next = pHead2;
        } else {
            p->next = pHead1;
        }
        return result->next;
    }
};

int main() {
    cout << Solution().Merge(buildList({1, 2, 3, 5, 6}), buildList({1, 2, 3, 4, 6})) << endl;
    return 0;
}